using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.Serialization;

namespace KPIS.GERP.Models.BGM
{
	[Table("bgm_project_operation_expense"), DataContract]
    public class ProjectOperationExpense
    {

        public ProjectOperationExpense()
        {
            //this.PrePurchaseRequestItems = new HashSet<ICM.PrePurchaseRequestItem>();
        }
    
		[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		[DataMember, Display(Name = "ProjOperExpId", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
		[Column("bgm_proj_oper_exp_seq", TypeName="int")]
        public int ProjOperExpId { get; set; }

        [ForeignKey("ProjectOperation")]
		[DataMember, Display(Name = "ProjOperId", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
        [Column("bgm_proj_oper_seq", TypeName="int")]
        public Nullable<int> ProjOperId { get; set; }

		[DataMember, Display(Name = "ProjOperExpDescr", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
        [Column("bgm_proj_oper_exp_descr", TypeName="ntext")]
        public string Descr { get; set; }

        [ForeignKey("ProjectExpense")]
		[DataMember, Display(Name = "BgmProjExpId", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
        [Column("bgm_proj_exp_seq", TypeName="int")]
        public Nullable<int> ProjExpId { get; set; }

		[DataMember, Display(Name = "Amount", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
		[Column("amount", TypeName="decimal")]
        public Nullable<decimal> Amount { get; set; }

		[DataMember, Display(Name = "UnitPrice", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
		[Column("unit_price", TypeName="decimal")]
        public Nullable<decimal> UnitPrice { get; set; }

		[DataMember, Display(Name = "UnitQty", ResourceType = typeof(Resources.ProjectOperationExpenseResource))]
		[Column("unit_qty", TypeName="int")]
        public Nullable<int> UnitQty { get; set; }

        //[DataMember]
        //public virtual ICollection<ICM.PrePurchaseRequestItem> PrePurchaseRequestItems { get; set; }

        [DataMember]
        public virtual ProjectOperation ProjectOperation { get; set; }

        [DataMember]
        public virtual ProjectExpense ProjectExpense { get; set; }


        [DataMember, NotMapped]
        //[Column("remain_amount", TypeName="decimal")]
        public decimal RemainAmount
        {
            get
            {
                if (_remainAmount.HasValue)
                    return _remainAmount.Value;
                using (var db = new GERPContext(false))
                {
                    var remainAmount = (from rpt in db.ProjectOperationReports
                                        where rpt.ProjOperExpId == this.ProjOperExpId 
                                        && rpt.RecordStatus != "D"
                                        select rpt.CalAmount).Sum();

                    return (remainAmount.HasValue ? remainAmount.Value : 0);
                    //return 0;
                }
            }
            set { _remainAmount = value; }
        }

        private decimal? _remainAmount { get; set; }

        #region RecordStatus

        [DataMember, Display(Name = "RecordStatus", ResourceType = typeof(SYS.Resources.RecordStatusResource))]
		[Column("record_status", TypeName="ntext")]
        public string RecordStatus { get; set; }

        [DataMember, Display(Name = "CreatedBy", ResourceType = typeof(SYS.Resources.RecordStatusResource))]
		[Column("created_by", TypeName="int")]
        public Nullable<int> CreatedBy { get; set; }

        [DataMember, Display(Name = "CreatedWhen", ResourceType = typeof(SYS.Resources.RecordStatusResource))]
		[Column("created_when", TypeName="datetime2")]
        public Nullable<System.DateTime> CreatedWhen { get; set; }

        [DataMember, Display(Name = "UpdatedBy", ResourceType = typeof(SYS.Resources.RecordStatusResource))]
		[Column("updated_by", TypeName="int")]
        public Nullable<int> UpdatedBy { get; set; }

        [DataMember, Display(Name = "UpdatedWhen", ResourceType = typeof(SYS.Resources.RecordStatusResource))]
		[Column("updated_when", TypeName="datetime2")]
        public Nullable<System.DateTime> UpdatedWhen { get; set; }

        #endregion

    }
}